package com.chenfan.magic.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenfan.magic.base.R;
import com.chenfan.magic.base.S;
import com.chenfan.magic.constant.MagicConstant;
import com.chenfan.magic.dao.ScriptsPublishMapper;
import com.chenfan.magic.entity.IdcScriptsPublish;
import com.chenfan.magic.service.IScriptsPublishService;
import com.chenfan.magic.utils.StringKit;
import com.chenfan.magic.utils.StringPool;
import com.chenfan.magic.utils.WebKit;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 服务实现类
 *
 * @author troy
 */
@Service
@AllArgsConstructor
public class ScriptsPublishServiceImpl extends ServiceImpl<ScriptsPublishMapper, IdcScriptsPublish> implements IScriptsPublishService {

    @Override
    public R get(String scriptIdOrIndexOrRoute) {
        if (StringKit.isBlank(scriptIdOrIndexOrRoute)) {
            return S.fail(S.MQL_CAN_NOT_FOUND);
        }
        boolean isIndex = scriptIdOrIndexOrRoute.contains("_");
        boolean isRoute = scriptIdOrIndexOrRoute.contains("/");
        IdcScriptsPublish idcScriptsPublish = null;
        if (isRoute) {
            //考虑支持restful风格: /rest / user / 1 => /rest / user / {userId}
            idcScriptsPublish = this.getOne(Wrappers.<IdcScriptsPublish>query().lambda().eq(IdcScriptsPublish::getSpScriptRoute, scriptIdOrIndexOrRoute));
        } else {
            if (isIndex) {
                idcScriptsPublish = this.getOne(Wrappers.<IdcScriptsPublish>query().lambda().eq(IdcScriptsPublish::getSpScriptIndex, scriptIdOrIndexOrRoute));
            } else {
                idcScriptsPublish = this.getById(scriptIdOrIndexOrRoute);
            }
        }
        if (idcScriptsPublish == null) {
            return S.fail(S.MQL_NOT_EXIST);
        } else {
            //脚本基础合法性判断
            if (StringKit.isBlank(idcScriptsPublish.getSpScriptContext())) {
                return S.fail(S.MQL_CONTEXT_NULL);
            }
            if (StringKit.isBlank(idcScriptsPublish.getSpScriptMethod())) {
                return S.fail(S.MQL_INVAILD_REST);
            }
            if (!(MagicConstant.COMMA + idcScriptsPublish.getSpScriptMethod() + MagicConstant.COMMA).contains(MagicConstant.COMMA + WebKit.getRequest().getMethod().toUpperCase() + MagicConstant.COMMA)) {
                return S.fail(S.MQL_NOT_MATCH_REST);
            }
            //魔方鉴权 => 鉴权通过后存redis, 尽可能降低网络请求带来的延迟
            if (StringPool.ONE.equals(idcScriptsPublish.getSpScriptIsNeedToken())) {
                //... 暂时关闭
            }
            //灰度发布
            if (StringPool.ONE.equals(idcScriptsPublish.getSpScriptPublishMode())) {
                if (!(MagicConstant.COMMA + idcScriptsPublish.getSpScriptGrayUnionIds() + MagicConstant.COMMA).contains(MagicConstant.COMMA + WebKit.getRequest().getHeader(StringPool.OPENID) + MagicConstant.COMMA)) {
                    return S.fail(S.MQL_GRAY_OPEN);
                }
            }
            return R.success().put(idcScriptsPublish);
        }
    }
}
